#!/usr/bin/env python
"""
Acorn-SDR (A Collaboration of Radio Nodes)
(c) Bob Cowdery (G3UKB) 2009

File: gtk_agc.py
Date: April 2009

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

The author can be reached by email at

bob@g3ukb.co.uk

========================================================================
Module Description:

Manages the agc pop-up window

"""

#-----------------------------------------------------------------------
# System imports
import sys
from time import sleep
import subprocess

# Import GTK libraries
import pygtk
import gtk
import gtk.glade
import gobject

# Application imports
import utils

#======================================================================
# The Filters pop-up.
class AgcGui:
  
    #---------------------------------------------------------------------
    def __init__(self, agc_tree, caps, dsp_pub, persist_service, callback):
        self.agc_tree = agc_tree
        self.caps = caps
        self.dsp_pub = dsp_pub
        self.persist_service = persist_service
        self.callback = callback
        self.ignore = False
        
        #-----------------------------------------------------------------------
        # Wire up the events
        dic =   {
            # Modes
            "on_tb_fixed_toggled" : self.agc_fixed,
            "on_tb_long_toggled" : self.agc_long,
            "on_tb_slow_toggled" : self.agc_slow,
            "on_tb_medium_toggled" : self.agc_medium,
            "on_tb_fast_toggled" : self.agc_fast,
            
            "on_b_cancel_agc_clicked" : self.cancel,
        }
        
        # This will connect up all the events in one hit
        self.agc_tree.signal_autoconnect (dic)
    
        # Now we need a reference to all the controls
        self.bfixed = self.agc_tree.get_widget('tb_fixed')
        self.blong = self.agc_tree.get_widget('tb_long')
        self.bslow = self.agc_tree.get_widget('tb_slow')
        self.bmedium = self.agc_tree.get_widget('tb_medium')
        self.bfast = self.agc_tree.get_widget('tb_fast')
        
        # Avoid circular events
        self.ignore = False
        
        self.w=self.agc_tree.get_widget('Agc')
        self.w.set_position(gtk.WIN_POS_MOUSE)

        # Widget map
        self.dict = {'AGC_FIXED':0,'AGC_LONG':1,'AGC_SLOW':2,'AGC_MEDIUM':3,'AGC_FAST':4}
        self.widgets = [self.bfixed, self.blong, self.bslow, self.bmedium, self.bfast]        
        
        # Adjust for capabilities
        for widget in self.widgets:
            widget.set_sensitive(False)
        allowed_agc_modes = self.caps.rsplit(',')
        for allowed_agc_mode in allowed_agc_modes:
            if allowed_agc_mode in self.dict:
                self.widgets[self.dict[allowed_agc_mode]].set_sensitive(True) 
    
    #--------------------------------------------------------------------- 
    # Public Interface
    #---------------------------------------------------------------------
    def show_window(self):
        self.w.show_all()
    
    #---------------------------------------------------------------------
    def hide_window(self):
        self.w.hide()
        
    #---------------------------------------------------------------------
    # GUI generated events (i.e. things that happen when you play with widgets)
    # Events for the mode buttons
    #---------------------------------------------------------------------  
    #---------------------------------------------------------------------  
    def agc_fixed(self, widget):
        if not self.ignore:
            self.updater('AGC_FIXED')
            self.w.hide()
    
    #---------------------------------------------------------------------  
    def agc_long(self, widget):
        if not self.ignore:
            self.updater('AGC_LONG')
            self.w.hide()
            
    #---------------------------------------------------------------------  
    def agc_slow(self, widget):
        if not self.ignore:
            self.updater('AGC_SLOW')
            self.w.hide()
            
    #---------------------------------------------------------------------  
    def agc_medium(self, widget):
        if not self.ignore:
            self.updater('AGC_MEDIUM')
            self.w.hide()
            
    #---------------------------------------------------------------------  
    def agc_fast(self, widget):
        if not self.ignore:
            self.updater('AGC_FAST')
            self.w.hide()
    
    #--------------------------------------------------------------------- 
    def updater(self, value):
        self.callback(value)
        
    #---------------------------------------------------------------------  
    def cancel(self, widget):
        self.w.hide()
        
    #---------------------------------------------------------------------  
    # Update events
    #---------------------------------------------------------------------  
    def agc_update(self, data):
        self.set_agc_hints(data)
    
    #---------------------------------------------------------------------
    # Private
    #---------------------------------------------------------------------
    def set_agc_hints(self, mode):
        self.ignore = True
        for n in range(len(self.widgets)):
            if n == self.dict[mode]:
                self.widgets[n].set_active(True)
            else:
                self.widgets[n].set_active(False)
        self.ignore = False

    